﻿//打印日志
function showlog(msg) {
    if (console) {
        console.log(new Date() + ">>"+msg);
    }
}

//检查系统
function checkClick() {
    var system = {
        win: false,
        mac: false,
        xll: false
    };
    var p = navigator.platform;
    system.win = p.indexOf("Win") !=-1;
    system.mac = p.indexOf("Mac") != -1;
    system.x11 = (p == "X11") || (p.indexOf("Linux") != -1);
    if (system.win || system.mac || system.xll) {
        G_CLICK = "click";
    } else {
        G_CLICK = "touchend"
    }
}

//点中状态
function addBgColor() {
    G_CLICKELEMENT && G_CLICKELEMENT.length > 0 && G_CLICKELEMENT.addClass("adedit_bg");
}

//离开状态
function removeBgColor() {
    G_CLICKELEMENT && G_CLICKELEMENT.length > 0 && G_CLICKELEMENT.removeClass("adedit_bg");
}

function changeFileText(txt)
{
    getfile().html(txt+"<input type=\"file\" />");
}

//上传文件
function uploadfile(event) {
    var img_file = event.target.files[0];
    if (!img_file) return false;

    if (!(/^image\/.*$/.test(img_file.type))) {
        alert('请选择图片文件');
        return false;
    }
    fReader = new FileReader();
    fReader.readAsDataURL(img_file);
    fReader.onload = function (e) {
        //是不是广告
        var gg = $(".adguanggaosave").length > 0;

        changeFileText('上传中...');
        $.post((gg?"/uploadguanggao":"/uploadimg"), { img: e.target.result }, function (ret) {
            if (ret.indexOf('|') != -1) {
                if(gg)
                    changeFileText('更换图片');
                else
                    changeFileText('继续上传');

                var as = ret.split('|');
                if (gg)
                    addGuangGaoIMG(as[1]);
                else
                    addTupianPan(as[1]);
            } else {
                changeFileText('重新上传');
            }

            bindButtonClickEvent();
        });
    };
}

//滚动条变动的时候
window.onscroll = function () {
    var a = document.documentElement.scrollTop == 0 ? document.body.clientHeight : document.documentElement.clientHeight;
    var b = document.documentElement.scrollTop == 0 ? document.body.scrollTop : document.documentElement.scrollTop;
    var c = document.documentElement.scrollTop == 0 ? document.body.scrollHeight : document.documentElement.scrollHeight;
    if (a + b >= c - 30) {

        showlog("lxw");
        if ($(".adtools").css("display") != "none") {
            var parentObj = $(".adtools");
            if (parentObj.find(".adnexturl").length < 1) {
                return
            }
            var fun = $(".adnexturl").attr("data-f");
            if (fun) eval(fun)
        }
    }
};


var _system={
    $:function(id){return document.getElementById(id);},
    _client:function(){
        return {w:document.documentElement.scrollWidth,h:document.documentElement.scrollHeight,bw:document.documentElement.clientWidth,bh:document.documentElement.clientHeight};
    },
    _scroll:function(){
        return {x:document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft,y:document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop};
    },
    _cover:function(show){
        if(show){
            this.$("adcover").style.display="block";
            this.$("adcover").style.width=(this._client().bw>this._client().w?this._client().bw:this._client().w)+"px";
            this.$("adcover").style.height=(this._client().bh>this._client().h?this._client().bh:this._client().h)+"px";
        }else{
            this.$("adcover").style.display="none";
        }
    },
    _guide:function(click){
        this._cover(true);
        this.$("adguide").style.display="block";
        this.$("adguide").style.top=(_system._scroll().y+5)+"px";
        window.onresize=function(){_system._cover(true);_system.$("adguide").style.top=(_system._scroll().y+5)+"px";};
        if(click){_system.$("adcover").onclick=function(){
            _system._cover();
            _system.$("adguide").style.display="none";
            _system.$("adcover").onclick=null;
            window.onresize=null;
        };}
    },
    _zero:function(n){
        return n<0?0:n;
    }
}